package com.test.practice.collection.map;

import java.util.HashMap;
import java.util.Map;

/**
 * Hash
 *
 * @Description: Hash
 * =======================
 * @Author: Jovi
 * =======================
 * @Date: Created in 2023-02-09
 * =======================
 * @Version: 1.1.2.0
 */
public class HashDemo {

    public static void main(String[] args) {
//        Map<String, String> map = new HashMap<>();

//        m1();

//        m2(17);

        m3();

    }

    /**
     * 这里相当于扩容的时候判断 原来值的hash后是在 原来老位置
     * 还是移动到新的扩容后的数组下面
     */
    private static void m3() {
        int oldHash = 13;
        int oldCap = 16;
        System.out.println("newHash 2进制：\t"+Integer.toBinaryString(oldHash));
        System.out.println("oldCap 2进制：\t"+Integer.toBinaryString(oldCap));
        if ((oldHash & oldCap) == 0) {
            System.out.println("老位置不变");
        }
        else {
            System.out.println("新位置");
        }
    }

    /**
     * 获取比cap大的2的n次幂
     * @param cap
     */
    private static void m2( int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        int size = (n < 0) ? 1 : n + 1;

        System.out.println(size);
    }

    /**
     * 模拟hashCode冲突
     */
    private static void m1() {
        HashDemo map = new HashDemo();

        map.put("六一","六一");
        map.put("六二","六一");
        map.put("六三","六一");
        map.put("张三","六一");
        map.put("李四","六一");
    }

    public void put(String key,String value) {
        System.out.printf("====:::key :%s \t key_hashCode :%s\t key 存储位置：%s\r\n",key,key.hashCode(),Math.abs(key.hashCode()%10));
    }
}
